using Dapr.AppCallback.Autogen.Grpc.v1;
using Dapr.Client.Autogen.Grpc.v1;
using Dcy.Dapr.Client.Models;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Newtonsoft.Json;

namespace Dcy.Dapr.Client.GrpcServices;

public class ClientRemoteService : AppCallback.AppCallbackBase
{
    private readonly ILogger<ClientRemoteService> _logger;

    public ClientRemoteService(ILogger<ClientRemoteService> logger)
    {
        _logger = logger;
    }


    public override async Task<InvokeResponse> OnInvoke(InvokeRequest request, ServerCallContext context)
    {
        _logger.LogInformation("OnInvoke Method {method}", request.Method);
        var response = new InvokeResponse();
        switch (request.Method)
        {
            default:
                break;
        }

        return response;
    }

    /// <summary>
    ///  绑定PubsubName和Topic
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override async Task<ListTopicSubscriptionsResponse> ListTopicSubscriptions(Empty request,
        ServerCallContext context)
    {
        _logger.LogInformation("ListTopicSubscriptions");
        var result = new ListTopicSubscriptionsResponse();
        result.Subscriptions.Add(new TopicSubscription
        {
            PubsubName = "orderpubsub",
            Topic = "orders",
        });
        return result;
    }

    /// <summary>
    /// 订阅Topic事件
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override async Task<TopicEventResponse> OnTopicEvent(TopicEventRequest request, ServerCallContext context)
    {
        _logger.LogInformation("OnTopicEvent PubsubName:{PubsubName} Topic:{Topic}", request.PubsubName, request.Topic);
        if (request.PubsubName == "orderpubsub")
        {
            var order = JsonConvert.DeserializeObject<Order>(request.Data.ToStringUtf8());
            switch (request.Topic)
            {
                case "orders":
                    _logger.LogInformation("OnTopicEvent {@input}", order);
                    break;
            }
        }

        return new TopicEventResponse();
    }

    /// <summary>
    /// 绑定Binding cron暂时没用
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override async Task<ListInputBindingsResponse> ListInputBindings(Empty request, ServerCallContext context)
    {
        _logger.LogInformation("ListInputBindings");
        var result = new ListInputBindingsResponse();
        result.Bindings.Add(["scheduled"]);
        return new ListInputBindingsResponse();
    }

    /// <summary>
    /// grpc Binding调用使用
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override Task<BindingEventResponse> OnBindingEvent(BindingEventRequest request, ServerCallContext context)
    {
        _logger.LogInformation("OnBindingEvent {name}",request.Name);
        switch (request.Name)
        {
            case "scheduled":
                _logger.LogInformation("OnBindingEvent switch {date}",DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"));
                break;
            default:
                break;
        }
        return base.OnBindingEvent(request, context);
    }
}